﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Chefbook.DataObjects;
using Chefbook.DataObjects.XmlSerialization;
using System.Xml.Linq;
using Chefbook.ViewModels;

namespace Chefbook.DataAccess
{
    /// <summary>
    /// Repository containing all recipes and categories
    /// </summary>
    public class ChefbookXmlRepository
    {
        static ChefbookXmlRepository instance = null;
        static readonly object padlock = new object();

        /// <summary>
        /// The static instance of ChefbookXmlRepository
        /// </summary>
        public static ChefbookXmlRepository Instance
        {
            get
            {
                lock (padlock)
                {
                    if (instance == null)
                    { instance = new ChefbookXmlRepository(); } return instance;
                }
            }
        }

        /// <summary>
        /// Dictionary of all recipes
        /// </summary>
        public Dictionary<Recipe, RecipeViewModel> Recipes { get; private set; }

        /// <summary>
        /// Dictionary of all categories
        /// </summary>
        public Dictionary<Category, CategoryViewModel> Categories { get; private set; }

        /// <summary>
        /// A list of all the root categores
        /// </summary>
        public List<CategoryViewModel> RootCategories { get; set; }

        /// <summary>
        /// Creates a new repository
        /// </summary>
        public ChefbookXmlRepository()
        {
            Recipes = new Dictionary<Recipe, RecipeViewModel>();
            Categories = new Dictionary<Category, CategoryViewModel>();
        }
        /// <summary>
        /// Creates a new repository from the specified XML files
        /// </summary>
        /// <param name="recipesPath">The path of the recipes XML file</param>
        /// <param name="categoriesPath">The path of the categories XML file</param>
        public ChefbookXmlRepository(string recipesPath, string categoriesPath)
        {
            XDocument recipeXml = XDocument.Load(recipesPath);
            XDocument categoriesXml = XDocument.Load(categoriesPath);
            Recipes = new Dictionary<Recipe, RecipeViewModel>();
            Categories = new Dictionary<Category, CategoryViewModel>();
            RootCategories = new List<CategoryViewModel>();
            foreach (var cat in XmlSerializer.DeserializeCategories(categoriesXml, recipeXml))
            {
                AddThisAndSubcategoriesToDictionary(cat);
                RootCategories.Add(Categories[cat]);
            }
            
        }

        private void AddThisAndSubcategoriesToDictionary(Category cat)
        {
            if (!Categories.Keys.Contains(cat))
            { Categories.Add(cat, new CategoryViewModel(cat, this)); }
            foreach (var item in cat.Subcategories)
            {
                AddThisAndSubcategoriesToDictionary(item);
            }
        }
    }
}